package shop

import (
	"errors"
	pkgApp "myserver/pkg/app"
	"myserver/pkg/constant"
	"net/http"

	"myserver/app/service/shop_service"
	"myserver/app/types"

	"github.com/gin-gonic/gin"
)

type ShopControllers struct {
}

// 新建商铺
func (ShopControllers) CeateShop(c *gin.Context) {
	var (
		m   types.UserShop
		app = pkgApp.Gin{C: c}
		ms  []types.UserShop
	)
	httpCode, _, err := pkgApp.BindAndValid(c, &m)

	if err != nil {
		app.Response(httpCode, err.Error(), nil)
		return
	}
	ms = append(ms, m)
	services := shop_service.ShopService{
		List: ms,
	}
	err = services.CeateShop()
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	app.Response(http.StatusOK, constant.SUCCESS, nil)
	// app.Response(http.StatusInternalServerError, "Size必须大于1", nil)
}
func (ShopControllers) UpDataShop(c *gin.Context) {
	var (
		app = pkgApp.Gin{C: c}
		ms  []types.UserShop
	)
	err := c.ShouldBindJSON(&ms)
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	if len(ms) == 0 {
		app.Response(http.StatusInternalServerError, errors.New("请传入店铺信息"), nil)
		return
	}
	services := shop_service.ShopService{
		List: ms,
	}
	err = services.UpDataShop()
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	app.Response(http.StatusOK, constant.SUCCESS, nil)
}
func (ShopControllers) List(c *gin.Context) {
	var (
		m        = types.ShopListStruct{}
		app      = pkgApp.Gin{C: c}
		listData = types.ShopListResponse{}
	)
	httpCode, errCode, err := pkgApp.BindAndValid(c, &m)
	if err != nil {
		app.Response(httpCode, errCode, err.Error())
		return
	}
	if m.Page == 0 {
		app.Response(http.StatusInternalServerError, "page不能为0", nil)
		return
	}
	if m.Size <= 0 {
		app.Response(http.StatusInternalServerError, "size必须大于0", nil)
		return
	}
	services := shop_service.ShopService{}
	ls, count, err := services.ListData(m.Page, m.Size, m.KeyWords)
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	ls1, err := pkgApp.ConvertDataByArr[types.UserShop](ls)
	if err != nil {
		app.Response(http.StatusInternalServerError, err.Error(), nil)
		return
	}
	listData.List = ls1
	listData.Total = count

	app.Response(http.StatusOK, constant.SUCCESS, listData)
}
